import { getEnums } from '@/api/common.js';
import { getDynamicData } from '@u/common.js';

const state = {},

  mutations = {
    SET_ENUM: (state, obj) => {
      if (obj.params && obj.params.isTree) {
        state['enumTree' + obj.id] = obj.res;
      } else {
        state['enum' + obj.id] = obj.res;
      }
    },
    SET_ISHAVE_ENUM: (state, obj) => {
      state[obj.key] = obj.value;
    },
  },

  actions = {
    async getEnum({ commit, state }, { id, params, value, callback }) {
      async function handleEnum(fn) {
        await getDynamicData(fn, {
          value,
          options: { childrenName: 'children' },
          callback: (res, isDel, arr) => {
            const res_tmp = JSON.parse(JSON.stringify(res));
            commit('SET_ENUM', { id: id, res: res, params: params });
            callback && callback(res_tmp, isDel, arr);
          }
        });
      }

      return new Promise(resolve => {
        if (params && params.isTree) { // 如果是树形的数据
          if (state['enumTree' + id] && state['enumTree' + id].length) {
            handleEnum(state['enumTree' + id]);
            resolve(state['enumTree' + id]);
          } else {
            if (state['boolTree' + id]) {
              handleEnum(state['enumTree' + id]);
              resolve(state['enumTree' + id]);
            } else {
              setTimeout(async() => {
                if (state['boolTree' + id]) {
                  handleEnum(state['enumTree' + id]);
                } else {
                  if (state['boolTree' + id] == undefined) {
                    commit('SET_ISHAVE_ENUM', {
                      key: 'boolTree' + id,
                      value: false,
                    });
                    await handleEnum(getEnums(id, { params }));
                  } else {
                    setTimeout(async() => {
                      if (state['boolTree' + id]) {
                        handleEnum(state['enumTree' + id]);
                      } else {
                        await handleEnum(getEnums(id, { params }));
                      }
                    }, 1000);
                  }
                }
                commit('SET_ISHAVE_ENUM', {
                  key: 'boolTree' + id,
                  value: true,
                });
                resolve(state['enumTree' + id]);
              }, state['boolTree' + id] == undefined ? 0 : 1000);
            }
          }
        } else {
          if (state['enum' + id] && state['enum' + id].length) {
            handleEnum(state['enum' + id]);
            resolve(state['enum' + id]);
          } else {
            if (state['bool' + id]) {
              handleEnum(state['enum' + id]);
              resolve(state['enum' + id]);
            } else {
              setTimeout(async() => {
                if (state['bool' + id]) {
                  handleEnum(state['enum' + id]);
                } else {
                  if (state['bool' + id] == undefined) {
                    commit('SET_ISHAVE_ENUM', {
                      key: 'bool' + id,
                      value: false,
                    });
                    await handleEnum(getEnums(id, { params }));
                  } else {
                    setTimeout(async() => {
                      if (state['bool' + id]) {
                        handleEnum(state['enum' + id]);
                      } else {
                        await handleEnum(getEnums(id, { params }));
                      }
                    }, 1000);
                  }
                }
                commit('SET_ISHAVE_ENUM', {
                  key: 'bool' + id,
                  value: true,
                });
                resolve(state['enum' + id]);
              }, state['bool' + id] == undefined ? 0 : 1000);
            }
          }
        }
      });
    },
    async updateEnum({ commit }, { id, params }) {
      if (params && params.isTree) { // 如果是树形的数据
        commit('SET_ISHAVE_ENUM', {
          key: 'boolTree' + id,
          value: undefined,
        });
      } else {
        commit('SET_ISHAVE_ENUM', {
          key: 'bool' + id,
          value: undefined,
        });
      }
      await getEnums(id, {
        params
      }).then(res => {
        if (res.code == 200) {
          commit('SET_ENUM', { id: id, res: res.data, params: params });
        } else {
          commit('SET_ENUM', { id: id, res: [], params: params });
        }
      });
    }
  };

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
